<?php

namespace app\model;

use think\Model;

class DebugLogs extends Model
{
    // 设置当前模型对应的数据表
    protected $name = 'debug_logs';
    // 设置当前模型主键
    protected $pk = 'id';

    /**
     * 根据条件查询数据是否存在
     * @param array $where
     * @param string $fields
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getInfoByWhere(array $where = [], string $fields = 'id')
    {
        $data = $this::where($where)->field($fields)->find();
        return $data ? $data->toArray() : [];
    }

    /**
     * 保存或者添加数据
     * @param $params
     * @return mixed
     */
    public function edit($params)
    {
        if (empty($params['id'])) { // 新增
            $params['created_at'] = date('Y-m-d H:i:s', time());
            $data = $this::create($params);
            return $data->id;
        } else { // 修改
            $params['updated_at'] = date('Y-m-d H:i:s', time());
        }
        $this::update($params);
        return $params['id'];
    }
}